<?php
	class Topic extends MY_controller{

		function __construct(){
			parent::__construct(); 
		}

		function index($text = 0,$limit = 0){
			$this->load->model("Topics");
			if($this->input->post()){
				$text = $this->input->post('text',TRUE) ? urldecode($this->input->post('text',TRUE)) : 0;
				$limit = 0;
			}
			$text = urldecode($text);
			$offset = 10;

			$result = $this->Topics->get_topic_list($limit, $offset, $text);
			$this->load->library('pagination');
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['base_url'] = '/topic/index/'.$text;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $offset;
			$page_config['uri_segment'] = 4;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("text", $text);
			$this->smarty->assign("topic_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("action", '题目列表');
			$this->smarty->display("topic/list.html");
		}

		private function save_analys($analys_data, $qid = 0){
			$data = array();
			foreach ($analys_data['analys_type'] as $key => $value) {
				if($value == 1){ //填空题
					if(!empty($analys_data['subject_text'][$key]) && !empty($analys_data['tk_answer_text'][$key])){
						$data[$key]['analys'] = array("analys_type" => $value, "subject_text" => $analys_data['subject_text'][$key]);
						$data[$key]['analys_result'][] = array("answer_text" => $analys_data['tk_answer_text'][$key]);
					}
				}else{//分录题
					if(!empty($analys_data['subject_text'][$key]) && !empty($analys_data['answer_text'][$key])){
						$data[$key]['analys'] = array("analys_type" => $value, "subject_text" => $analys_data['subject_text'][$key]);
						foreach ($analys_data['answer_text'][$key] as $k => $answer_text) {
							$answer_text = trim($answer_text);
							$type = $analys_data['type'][$key][$k];
							$topic_subject_id = $analys_data['topic_subject_id'][$key][$k];
							$topic_subject_title = $analys_data['topic_subject_title'][$key][$k];
							if(!empty($answer_text) && !empty($type) && !empty($topic_subject_id)){
								$data[$key]['analys_result'][] = array("answer_text" => $answer_text, "type" => $type, "topic_subject_id" => $topic_subject_id, "topic_subject_title" => $topic_subject_title);
							}
						}
					}
				}
			}
			return $data;
		}

		/**
		 * 添加题目
		 */
		function add(){
			if($_POST){
				$post_data = $this->input->post();
				if(empty($post_data['subject_text'])){
					show_message($_SERVER['HTTP_REFERER'], "请填写题目内容");exit;
				}

				$post_data['add_time'] = time();
				if(isset($post_data['file'])) unset($post_data['file']);
				//has_sub_topic=1是保存主题目并添加子题目，has_sub_topic=0是保存主题目并直接添加答案（不添加子题目）
				$post_data['has_sub_topic'] = 1; 
				if(isset($post_data['save_add_items'])){
					unset($post_data['save_add_items']);
					$post_data['has_sub_topic'] = 1;
				}
				if(isset($post_data['save_add_answers'])){
					unset($post_data['save_add_answers']);
					$post_data['has_sub_topic'] = 0;
				}
				$this->db->insert("tk_topics", $post_data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
					if($post_data['type'] == 6){
						//计算题系统自己添加答案
						$post_data['subject_text'] = str_replace('÷', '/', $post_data['subject_text']);
						$post_data['subject_text'] = str_replace('×', '*', $post_data['subject_text']);
						$post_data['subject_text'] = str_replace('－', '-', $post_data['subject_text']);
						$post_data['subject_text'] = str_replace('＋', '+', $post_data['subject_text']);
						$post_data['subject_text'] = str_replace('（', '(', $post_data['subject_text']);
						$post_data['subject_text'] = str_replace('）', ')', $post_data['subject_text']);
						$answer_text = eval("return {$post_data['subject_text']};");
						$result_data['topic_id'] = $insert_id;
						$result_data['answer_text'] = $answer_text;
						$result_data['is_right'] =  1;
						$this->db->insert("tk_topic_result", $result_data);
						header("Location:/topic/index");exit;
						// show_message('/topic/index');exit;
					}else{
						redirect("topic/save_topic_item/" . $insert_id);
					}
				}
			}else{
				$this->load->model("Areas");				
				//获取科目
				$this->load->model('Subject_model');
				$subject = $this->Subject_model->get_subject_all();
				$this->smarty->assign("subject", $subject);

				$province_list = $this->Areas->get_province_data();
				$this->smarty->assign("action", '添加题目');
				
				$this->load->model('Subject_book_model');
				$book_list = $this->Subject_book_model->get_book_list();
				$letter = array('A','B','C','D');
				$this->smarty->assign("book_list", $book_list['result']);
				$city_list = $this->Areas->get_province_data(440000);
				$this->smarty->assign("city_list", $city_list);
				$chapter_list_data = $this->get_chapter_list(27);
				// print_r($chapter_list_data);
				$this->smarty->assign("chapter_list_data", $chapter_list_data);
				$this->smarty->assign("province_list", $province_list);
				$this->smarty->assign("post_url", '/topic/add');
				$this->smarty->display("topic/save_topic.html");
			}
		}

		//添加计算题
		function add_calc(){
			if($_POST){
				$post_data = $this->input->post();
				$text = array();
				if(empty($post_data['text'])){
					show_message("/topic/add_calc", "没有算式");exit;
				}else{
					$text = $post_data['text'];
					unset($post_data['text']);
				}
				$post_data['add_time'] = time();
				if(isset($post_data['file'])) unset($post_data['file']);
				//has_sub_topic=1是保存主题目并添加子题目，has_sub_topic=0是保存主题目并直接添加答案（不添加子题目）
				$post_data['has_sub_topic'] = 0; 
				$post_data['subject_text'] = '';
				foreach ($text as $key => $value) {
					$post_data['subject_text'] = $post_data['subject_text'] . $value;
				}
				$this->db->insert("tk_topics", $post_data);
				$insert_id = $this->db->insert_id();
				// $insert_id = 1;
				$fh = array(1 => "+", 2 => "-", 3 => "*", 4 => "/", 5 => "(", 6 => ")");
				$topic_calc_data = array();
				$suanshi = '';
				foreach ($text as $key => $value) {
					$data['topic_id'] = $insert_id;
					$index = array_search($value, $fh);
					if($index > 0){
						$data['type'] = 2;
						$data['value'] = $index;
					}else{
						$data['type'] = 1;
						$data['value'] = $value;
					}
					if($data){
						$topic_calc_data[] = $data;
					}
					$suanshi = $suanshi . $value;
				}
				if($suanshi != ''){
					$topic_result['topic_id'] = $insert_id;
					$topic_result['answer_text'] = eval("return {$suanshi};");;
					$topic_result['is_right'] = 1;
					$this->db->insert("tk_topic_result", $topic_result);
					if(count($topic_calc_data) > 0){
						$this->db->insert_batch("tk_topics_calc", $topic_calc_data);
						// show_message("/topic/add_calc", "添加题目成功");exit;
						header("Location:/topic/index");exit;
					}else{
						show_message("/topic/add_calc", "添加失败，请检查题目内容");exit;
					}
				}else{
					show_message("/topic/add_calc", "未知错误，请检查题目内容");exit;
				}
			}
			$this->load->model('Subject_model');
			$subject = $this->Subject_model->get_subject_all();
			$this->smarty->assign("subject", $subject);

			
			$this->load->model('Subject_book_model');
			$book_list = $this->Subject_book_model->get_book_list();
			$letter = array('A','B','C','D');
			$this->smarty->assign("book_list", $book_list['result']);
			$chapter_list_data = $this->get_chapter_list(27);
			$this->smarty->assign("chapter_list_data", $chapter_list_data);
			$this->smarty->assign("action", '添加计算题');
			$this->smarty->display("topic/save_topic_calc.html");
		}

		function add_tiankong(){
			if($_POST){
				$post_data = $this->input->post();
				if(empty($post_data['an_tiakong'])){
					show_message("/topic/add_tiankong", "没有任何答案");
					exit;
				}
				

				//处理题目HTML内容
				$sub_text = $post_data['subject_text'];
				$sub_text = cleanhtml($sub_text);//过滤img以外的html标签
				$sub_text = str_replace('&nbsp;','',$sub_text);
				
				$regex1 = '/（[?=\s]+）/';
				//把括号替换成下划线
				$sub_text = preg_replace($regex1, "____", $sub_text); 
				//重新赋值
				$post_data['subject_text'] = $sub_text;
				$an_tiakong = $post_data['an_tiakong'];
				unset($post_data['an_tiakong'],$post_data['file']);
				$this->db->insert("tk_topics", $post_data);
				$insert_id = $this->db->insert_id();;

				$tiankong_answer = array();
				foreach ($an_tiakong as $key => $answer_list) {
					if($key == 'symbol'){
						$type = 2;
					}elseif($key == 'number'){
						$type = 3;
					}else{
						$type = 1;
					}
					foreach ($answer_list as $k => $v) {
						$answer_data['topic_id'] = $insert_id;
						$answer_data['answer_text'] = $v;
						$answer_data['is_right'] = 1;
						$answer_data['type'] = $type;
						$tiankong_answer[] = $answer_data;
					}
					
				}
				$this->db->insert_batch("tk_topic_result", $tiankong_answer);
				// show_message("/topic/add_tiankong", "添加题目成功");
				header("Location:/topic/index");exit;
				exit;
			}
			$this->load->model('Subject_book_model');
			$book_list = $this->Subject_book_model->get_book_list();
			$letter = array('A','B','C','D');
			$this->smarty->assign("book_list", $book_list['result']);
			$chapter_list_data = $this->get_chapter_list(27);
			$this->smarty->assign("chapter_list_data", $chapter_list_data);
			$this->smarty->assign("action", '添加填空题');
			$this->smarty->display("topic/save_topic_tiankong.html");
		}



		//添加混合题
		function add_blend(){

			if($_POST){
				$post_data = $this->input->post();
				$text = array();
				// if(empty($post_data['daan_array'])){
				// 	show_message("/topic/add_blend", "没有算式");exit;
				// }
				// $result_list = $post_data['daan_array'];
				// unset($post_data['daan_array']);
				if(!empty($post_data['text'])){
					$text = $post_data['text'];
					unset($post_data['text']);
				}
				$post_data['add_time'] = time();
				if(isset($post_data['file'])) unset($post_data['file']);
				//has_sub_topic=1是保存主题目并添加子题目，has_sub_topic=0是保存主题目并直接添加答案（不添加子题目）
				$post_data['has_sub_topic'] = 0; 
				// $post_data['subject_text'] = '';
				// foreach ($text as $key => $value) {
				// 	$post_data['subject_text'] = $post_data['subject_text'] . $value;
				// }
				// print_r($post_data['subject_text']);exit;
				// print_r($text);exit;
				// 添加题目
				$this->db->insert("tk_topics", $post_data);
				$insert_id = $this->db->insert_id();
			
				// $fh = array(1 => "+", 2 => "-", 3 => "*", 4 => "/", 5 => "(", 6 => ")",7=>"=");
				// $topic_calc_data = array();
				// $suanshi = '';
				// foreach ($text as $key => $value) {
				// 	// $data['topic_id'] = $insert_id;
				// 	$index = array_search($value, $fh);
				// 	if($index > 0){
				// 		$data['type'] = 2;
				// 		$data['value'] = $index;
				// 	}else{
				// 		$data['type'] = 1;
				// 		$data['value'] = $value;
				// 	}
				// 	if($data){
				// 		$topic_calc_data[] = $data;
				// 	}
				// 	$suanshi = $suanshi . $value;
				// }
				// print_r($post_data['daan_array']);exit;
				foreach ($text as $key => $value) {
					$result = array(
						'topic_id' => $insert_id, 
						'topic_item_id' => 0, 
						'answer_text' => $value, 
						'is_right' => 1, 
						'type' => 4, 
						);
					$this->db->insert('tk_topic_result',$result);
				}
				// show_message("/topic/add_blend", "添加题目成功");exit;
				header("Location:/topic/index");exit;
				
				// if($suanshi != ''){
				// 	$topic_result['topic_id'] = $insert_id;
				// 	$topic_result['answer_text'] = eval("return {$suanshi};");;
				// 	$topic_result['is_right'] = 1;
				// 	$this->db->insert("tk_topic_result", $topic_result);
				// 	if(count($topic_calc_data) > 0){
				// 		$this->db->insert_batch("tk_topics_calc", $topic_calc_data);
				// 		show_message("/topic/add_calc", "添加题目成功");exit;
				// 	}else{
				// 		show_message("/topic/add_calc", "添加失败，请检查题目内容");exit;
				// 	}
				// }else{
				// 	show_message("/topic/add_calc", "未知错误，请检查题目内容");exit;
				// }
			}


			//科目
			$this->load->model('Subject_model');
			$subject = $this->Subject_model->get_subject_all();
			$this->smarty->assign("subject", $subject);

			//课本
			$this->load->model('Subject_book_model');
			$book_list = $this->Subject_book_model->get_book_list();
			$this->smarty->assign("book_list", $book_list['result']);

			//章节
			$chapter_list_data = $this->get_chapter_list(27);
			$this->smarty->assign("chapter_list_data", $chapter_list_data);
			$this->smarty->assign("action", '添加算式题');


			$this->smarty->assign("post_url", '/topic/add_blend');
			$this->smarty->display("topic/save_topic_blend.html");
		}

		function save_topic_item($topic_id = 0, $item_id = 0, $has_sub_topic = 0){
			$this->load->model("Topics");
			if(empty($topic_id)){
				show_message("/topic", "非法操作：缺少题目ID");
				exit;
			}
			$letter = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
			$this->smarty->assign("letter", $letter);
			$topic_info = get_field_by_where("tk_topics", "*", "id='{$topic_id}'");
			if($topic_info['has_sub_topic'] == 0){
				$topic_result_list = $this->Topics->get_result_by_topic_id($topic_id);
				$this->smarty->assign("topic_result", $topic_result_list);
			}
			if(!$topic_info){
				show_message("/topic", "题目不存在");
				exit;
			}
			if($_POST){
				$post_data = $this->input->post();
				// print_r($post_data);exit;
				if(empty($post_data['an_checkbox_text']) && $topic_info['has_sub_topic'] == 1){
					show_message($_SERVER['HTTP_REFERER'], "请填写题目内容");exit;
				}
				if($topic_info['has_sub_topic'] == 1){
					$data['topic_id'] = $topic_id;
					$data['type'] = $post_data['type'];
					$data['subject_text'] = $post_data['subject_text'];
					$data['analysis'] = $post_data['analysis'];
					if($data['type'] == 1){
						if(empty($post_data['an_radio'])){
							show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
						}
						if(empty($post_data['an_radio_text'])){
							show_message($_SERVER['HTTP_REFERER'], "请填写题目答案");exit;
						}
					}elseif($data['type'] == 2){
						/*if(empty($post_data['an_checkbox'])){
							show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
						}*/
						if(empty($post_data['an_checkbox_text'])){
							show_message($_SERVER['HTTP_REFERER'], "请填写题目答案");exit;
						}
					}elseif($data['type'] == 3){
						if(empty($post_data['an_choice'])){
							show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
						}
					}elseif($data['type'] == 4){
						if(empty($post_data['analysis'])){
							show_message($_SERVER['HTTP_REFERER'], "名词解释、简答题的解析内容不能为空");exit;
						}
					}/*elseif($data['type'] == 4){
						if(empty($post_data['analys'])){
							show_message($_SERVER['HTTP_REFERER'], "至少需要一个子题干");exit;
						}
						$analys_data = $this->save_analys($post_data['analys']);
						if(!$analys_data){
							show_message($_SERVER['HTTP_REFERER'], "子题目是空的");
							exit;
						}
					}*/

					$this->db->insert("tk_topic_items", $data);
					$insert_id = $this->db->insert_id();
					$result_data['topic_item_id'] = $insert_id;
				}else{
					$insert_id = $topic_info['id'];
					$data['type'] = $topic_info['type'];
					if($data['type'] == 1){
						/*if(empty($post_data['an_radio'])){
							show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
						}*/
						if(empty($post_data['an_radio_text'])){
							show_message($_SERVER['HTTP_REFERER'], "请填写题目答案");exit;
						}
					}elseif($data['type'] == 2){
						/*if(empty($post_data['an_checkbox'])){
							show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
						}*/
						if(empty($post_data['an_checkbox_text'])){
							show_message($_SERVER['HTTP_REFERER'], "请填写题目答案");exit;
						}
					}elseif($data['type'] == 3){
						if(empty($post_data['an_choice'])){
							show_message($_SERVER['HTTP_REFERER'], "请选择一个正确答案");exit;
						}
					}elseif($data['type'] == 4){
						if(empty($post_data['analysis'])){
							show_message($_SERVER['HTTP_REFERER'], "名词解释、简答题的解析内容不能为空");exit;
						}
					}
					$result_data['topic_id'] = $insert_id;
				}
				
				if($insert_id > 0){
					//插入答案数据
					if($data['type'] == 1){
						foreach ($post_data['an_radio_text'] as $radio_key => $radio_val) {
							$result_data['answer_text'] = $radio_val;
							$result_data['is_right'] =  ( !empty($post_data['an_radio']) && $post_data['an_radio'] == $radio_key) ? 1 : 0;
							if($result_data['is_right'] == 1){
								$this->db->where("is_right", 1);
								$this->db->where("topic_id", $insert_id);
								$this->db->update("tk_topic_result", array("is_right" => 0));
							}
							$this->db->insert("tk_topic_result", $result_data);
						}
					}elseif($data['type'] == 2){
						foreach ($post_data['an_checkbox_text'] as $checkbox_key => $checkbox_val) {
							$result_data['answer_text'] = $checkbox_val;
							$result_data['is_right'] =  ( isset($post_data['an_checkbox'][$checkbox_key])) ? 1 : 0;
							$this->db->insert("tk_topic_result", $result_data);
						}
					}elseif($data['type'] == 3){
						$result_data['answer_text'] = '正确';
						$result_data['is_right'] =  ( $post_data['an_choice'] == 1) ? 1 : 0;
						$this->db->insert("tk_topic_result", $result_data);

						$result_data['answer_text'] = '错误';
						$result_data['is_right'] = ( $post_data['an_choice'] == 2) ? 1 : 0;
						$this->db->insert("tk_topic_result", $result_data);
					}elseif($data['type'] == 4){
						/*foreach ($analys_data as $key => $topic_analys_data) {
							$topic_analys_data['analys']['topic_item_id'] = $insert_id;
							$this->db->insert("tk_topic_analys", $topic_analys_data['analys']);
							$aid = $this->db->insert_id();
							$topic_analys_result_data = array();
							foreach ($topic_analys_data['analys_result'] as $k => $topic_analys_result) {
								$topic_analys_result['aid'] = $aid;
								$topic_analys_result['topic_item_id'] = $insert_id;
								$topic_analys_result_data[] = $topic_analys_result;
							}
							$this->db->insert_batch("tk_topic_analys_result", $topic_analys_result_data);
						}*/
					}elseif($data['type'] == 5){
						foreach ($post_data['an_tiakong'] as $tiakong_key => $tiakong_val) {
							$result_data['answer_text'] = $tiakong_val;
							$result_data['is_right'] =  1;
							$this->db->insert("tk_topic_result", $result_data);
						}
					}elseif($data['type'] == 6){
						$answer_text = eval("return {$post_data['subject_text']};");
						$result_data['answer_text'] = $answer_text;
						$result_data['is_right'] =  1;
						$this->db->insert("tk_topic_result", $result_data);
					}
 					// show_message('/topic/add');exit;
 					header("Location:/topic/index");exit;
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");exit;
 				}
			}else{
				if(!empty($item_id)){
					$topic_info = get_field_by_where("tk_topic_items", "*", "id='{$item_id}'");
					$topic_result = $this->Topics->get_topic_reuslt($item_id);
					$letter = array('A','B','C','D');
					$this->smarty->assign("topic_info", $topic_info);
					$this->smarty->assign("topic_result", $topic_result);
					$this->smarty->assign("letter", $letter);
				}
			}
			$this->load->model("Topics");
			$this->load->model("Areas");
			$this->load->model('Subject_model');
			$this->load->model('Subject_book_model');
			$subject = $this->Subject_model->get_subject_all();
			$book_list = $this->Subject_book_model->get_book_list();
			$chapter_list_data = $this->get_chapter_list($topic_info['book_id']);
			$letter = array('A','B','C','D');
			$this->smarty->assign("t_topic_info", $topic_info);
			$this->smarty->assign("action", '编辑题目');
			$this->smarty->assign("subject", $subject);
			$this->smarty->assign("book_list", $book_list['result']);
			$this->smarty->assign("chapter_list_data", $chapter_list_data);
			$this->smarty->assign("action", '添加题目');
			$this->smarty->assign("has_sub_topic", $has_sub_topic);
			$this->smarty->display("topic/save_topic_item.html");
		}

		function edit($id = 0){
			if($_POST){
				$post_data = $this->input->post();
				if(isset($post_data['file'])) unset($post_data['file']);
				$this->db->where('id', $post_data['id']);
 				$this->db->update('tk_topics', $post_data);
 				if($this->db->affected_rows() > 0){
 					show_message($_SERVER['HTTP_REFERER']);
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");
 				}
			}else{
				if(intval($id) > 0){
					$topic_info = get_field_by_where("tk_topics", "*", "id='{$id}'");
					if(!$topic_info){
						show_message("/news/type_list", "数据不存在");exit;
					}
					$this->load->model("Topics");
					$this->load->model("Areas");
					$this->load->model('Subject_model');
					$this->load->model('Subject_book_model');
					$subject = $this->Subject_model->get_subject_all();
					
					$book_list = $this->Subject_book_model->get_book_list();
					// $book_list = $this->Categorys->get_category_data();
					// $topic_result = $this->Topics->get_topic_reuslt($id);
					$chapter_list_data = $this->get_chapter_list($topic_info['book_id']);
					/*if($topic_info['type'] == 4){
						$topic_result = $this->Topics->get_topic_analys($id);
						if($topic_result){
							foreach ($topic_result as $key => $value) {
								$topic_result[$key]['result'] = $this->Topics->get_topic_analys_result($value['id']);
							}
						}
					}else{
						$topic_result = $this->Topics->get_topic_reuslt($id);
					}*/
					
					$this->smarty->assign("topic_info", $topic_info);
					$this->smarty->assign("action", '编辑题目');
					$this->smarty->assign("subject", $subject);
					$this->smarty->assign("book_list", $book_list['result']);
					$this->smarty->assign("chapter_list_data", $chapter_list_data);
					// $this->smarty->assign("topic_result", $topic_result);
					$this->smarty->assign("post_url", '/topic/edit');
					$this->smarty->display("topic/save_topic.html");
				}else{
					show_message("/news", "非法操作");
				}
			}
		}

		function show_items($topic_id = 0){
			if(empty($topic_id)){
				show_message("/topic", "非法操作：缺少题目ID");
				exit;
			}
			$topic_info = get_field_by_where("tk_topics", "*", "id='{$topic_id}'");
			if(!$topic_info){
				show_message("/topic", "题目不存在");
				exit;
			}
			$this->load->model("Topics");
			$items = $this->Topics->get_topic_items($topic_id);
			$this->smarty->assign("items", $items);
			$this->smarty->assign("action", '子题目列表');
			$this->smarty->display("topic/show_items.html");
		}

		function get_chapter_list($subject_id){
			$this->load->model('Subject_book_model');
			$chapter_list = $this->Subject_book_model->get_chapter_list_by_book($subject_id);
			$chapter_list_data = array();
			$code = 0;
			if($chapter_list){
				foreach ($chapter_list as $key => $value) {
					if($value['parent_id'] == 0){
						$chapter_list_data[$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				foreach ($chapter_list as $key => $value) {
					if(isset($chapter_list_data[$value['parent_id']])){
						$chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				if($chapter_list AND $chapter_list_data){
					foreach ($chapter_list_data as $key => $value) {
						if(isset($value['sub'])){
							foreach ($value['sub'] as $k => $v) {
								foreach ($chapter_list as $k1 => $v1) {
									if($k == $v1['parent_id']){
										$chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
									}
								}
							}
						}
					}
				}
			}
			return $chapter_list_data;
		}

		/**
		 * AJAX获取课本
		 * @return [type] [description]
		 */
		function ignore_get_books(){
			$post_data = $this->input->post();
			if(is_ajax_request()){
				$this->load->model('Subject_book_model');
				
				$chapter_list_data = $this->Subject_book_model->get_book_list_by_subject($post_data['subject_id']);
				$code = 0;
				if($chapter_list_data){
					$code = 1;
				}
				echo json_encode(array("code" => $code, "chapter_list" => $chapter_list_data));
			}
		}

		/**
		 * AJAX获取章节
		 * @return [type] [description]
		 */
		function ignore_get_book_chapter(){
			$post_data = $this->input->post();
			if(is_ajax_request()){
				$this->load->model('Subject_book_model');
				$chapter_list = $this->Subject_book_model->get_chapter_list_by_book($post_data['book_id']);
				$chapter_list_data = array();
				if($chapter_list){
					foreach ($chapter_list as $key => $value) {
						if($value['parent_id'] == 0){
							$chapter_list_data[$value['id']] = $value;
							unset($chapter_list[$key]);
						}
					}

					foreach ($chapter_list as $key => $value) {
						if(isset($chapter_list_data[$value['parent_id']])){
							$chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
							unset($chapter_list[$key]);
						}
					}

					if($chapter_list AND $chapter_list_data){
						foreach ($chapter_list_data as $key => $value) {
							if(isset($value['sub'])){
								foreach ($value['sub'] as $k => $v) {
									foreach ($chapter_list as $k1 => $v1) {
										if($k == $v1['parent_id']){
											$chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
										}
									}
								}
							}
						}
					}
				}
				$code = 0;
				if($chapter_list_data){
					$code = 1;
				}
				echo json_encode(array("code" => $code, "chapter_list" => $chapter_list_data));
			}
		}

		function delete($ids = 0){
			if($_POST){
				$ids = $this->input->post('ids');
			}else{
				$ids = (array) $ids;
			}
			delete_data($ids, "tk_topics");
			if($this->db->affected_rows() > 0){
				// show_message($_SERVER['HTTP_REFERER'], "删除成功");
				header("Location:/topic/index");exit;
			}else{
				show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
			}
		}

		function import(){
			$this->smarty->assign("action", '导入题目');
			$this->smarty->display("topic/import.html");
		}

		function ignore_del_topic_result(){
			if(is_ajax_request()){
				$post_data = $this->input->post();
				delete_data(array($post_data['resultid']), "tk_topic_result");
				if($this->db->affected_rows() > 0){
					echo json_encode(array("error" => 0));
					exit;
				}else{
					echo json_encode(array("error" => 1, "msg" => "删除失败，请重试！"));
					exit;
				}
			}
		}
	}
